package com.zhongya.example.fragment.myview.study2

import android.graphics.Color
import android.util.Log
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhongya.example.R
import com.zhongya.example.adapter.Part7AdapterItemTouchHelper
import com.zhongya.example.databinding.ExampleFragmentPart7Binding
import com.zhongya666.library.base.fragment.BaseDbFragment
import java.util.*

class Part7Fragment : BaseDbFragment<ExampleFragmentPart7Binding>(R.layout.example_fragment_part7) {

    val list = mutableListOf<String>()
    val adapter = Part7AdapterItemTouchHelper()

    override fun initView() {

        for (i in 0 .. 10){
            list.add("$i")
        }

        mDatabind.rv.layoutManager = LinearLayoutManager(context)

        mDatabind.rv.adapter =adapter
        mDatabind.rv.addItemDecoration(DividerItemDecoration(context,DividerItemDecoration.VERTICAL))
        val itemTouchHelper = ItemTouchHelper(MyHelper())
        itemTouchHelper.attachToRecyclerView(mDatabind.rv)

        adapter.setList(list)

        adapter.setDragListener {
//            itemTouchHelper.startDrag(it)
            itemTouchHelper.startSwipe(it)
        }

    }

    override fun createObserver() {

    }

    override fun lazyLoadData() {

    }

    inner class MyHelper : ItemTouchHelper.Callback() {
        override fun getMovementFlags(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder
        ): Int {
            val dargFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
            val swipeFlags = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
            return makeMovementFlags(dargFlags, swipeFlags)
        }

        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            val fromPosition = viewHolder.adapterPosition
            val toPosition = target.adapterPosition
            Collections.swap(list,fromPosition,toPosition)
            adapter.notifyItemMoved(fromPosition,toPosition)
            return true
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            list.removeAt(viewHolder.adapterPosition)
            adapter.notifyItemRemoved(viewHolder.adapterPosition)

            list.forEach{
                Log.e("wzy","$it")
            }
        }

        override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
            super.onSelectedChanged(viewHolder, actionState)
            if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE){
                val bgColor = viewHolder?.itemView?.context?.resources?.getColor(R.color.red)
                if (bgColor != null) {
                    viewHolder.itemView?.setBackgroundColor(bgColor)
                }
            }else if (actionState == ItemTouchHelper.ACTION_STATE_DRAG){
                val bgColor = viewHolder?.itemView?.context?.resources?.getColor(R.color.green)
                if (bgColor != null) {
                    viewHolder.itemView?.setBackgroundColor(bgColor)
                }
            }
        }

        override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
            super.clearView(recyclerView, viewHolder)
            viewHolder.itemView?.setBackgroundColor(Color.WHITE)
        }

        override fun isLongPressDragEnabled(): Boolean {
            return false
        }

        override fun isItemViewSwipeEnabled(): Boolean {
            return false
        }


    }
}